package com.bianguo.android.bprinter.view.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.baselibrary.view.BaseActivity;
import com.bianguo.android.bprinter.R;
import com.bianguo.android.bprinter.view.widget.AddWordFrame;
import com.bianguo.android.bprinter.view.widget.DBoardView;
import com.bianguo.android.bprinter.view.widget.TextStickerView;
import com.bianguo.android.bprinter.view.widget.ZoomFrameView;
import com.bianguo.android.bprinter.view.widget.ZoomLayout;
import com.bianguo.android.bprinter.view.widget.ZoomView;
import com.bianguo.android.bprinter.view.widget.ZoomViewLayout;
import com.bianguo.android.bprinter.view.widget.ZoomViewLayout2;
import com.bianguo.android.ioclibrary.annotations.BindView;
import com.bianguo.android.ioclibrary.annotations.OnClick;

import java.io.File;
import java.util.ArrayList;

public class DoodingActivity extends BaseActivity implements ZoomFrameView.OnClickIconListener, ZoomView.OnClickPointListener, ZoomView.OnFocusListener, ZoomView.OnDoubleClickListener {

    @BindView(R.id.board_view)
    DBoardView boardView;
    @BindView(R.id.dooding_zoom_view)
    ZoomFrameView zoomFrameView;
    @BindView(R.id.dooding_zoom_layout)
    ZoomLayout zoomLayout;
    @BindView(R.id.text_sticker_view)
    TextStickerView stickerView;
    @BindView(R.id.zoom_view)
    ZoomView zoomView;
    @BindView(R.id.zoom_view2)
    ZoomView zoomView2;
    @BindView(R.id.zoom_view_layout)
    ZoomViewLayout2 zoomViewLayout;
    @BindView(R.id.add_word_frame)
    AddWordFrame addWordFrame;

    @Override
    protected int layoutId() {
        return R.layout.activity_dooding;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        zoomFrameView.setOnClickIconListener(this);
        stickerView.setText("你好世界");
        stickerView.setTextColor(Color.BLACK);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_cat);
        zoomView.addBitmap(bitmap);
        zoomView.setOnClickPointListener(this);
        zoomView.setOnFocusListener(this);
        zoomView.setOnDoubleClickListener(this);

        zoomView2.addBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.d_haixiu));
        zoomView2.setOnFocusListener(this);

        String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(absolutePath, "aaa.jpg");
        if (file.exists()) {
            zoomViewLayout.addZoomView(BitmapFactory.decodeFile(file.getAbsolutePath()), true);
        }
        zoomViewLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.test_cat), true);
        zoomViewLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.d_haixiu), true);
        zoomViewLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.test_cat), true);
        zoomViewLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.d_haixiu), true);


        addWordFrame.setSelect(true);
    }

    @OnClick({R.id.redo_btn, R.id.unredo_btn, R.id.erase_btn,
            R.id.save_btn})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.redo_btn:
                boardView.recallDrawPath();
                break;
            case R.id.unredo_btn:
                boardView.recallRecallPath();
                break;
            case R.id.erase_btn:
                switch (boardView.getMode()) {
                    case ERASE:
                        boardView.setMode(DBoardView.Mode.DRAW);
                        break;
                    case DRAW:
                        boardView.setMode(DBoardView.Mode.ERASE);
                        break;
                }
                break;
            case R.id.save_btn:
                zoomView.addBitmap(boardView.saveBitmap());
                break;
        }
    }

    @Override
    public void onClickIcon(View view, int iconPosition) {
        switch (iconPosition) {
            case ZoomFrameView.ICON_RIGHT_TOP:
                showToast("镜像");
                break;
            case ZoomFrameView.ICON_LEFT_TOP:
                showToast("删除");
                break;
            case ZoomFrameView.ICON_LEFT_BOTTOM:
                showToast("剪切");
                break;
        }
    }

    @Override
    public void onClickPoint(View view, int pointType) {
        switch (pointType) {
            case ZoomView.CLICK_POINT_TYPE_DELETE:
                showToast("删除");
                ViewGroup parentView = (ViewGroup) view.getParent();
                parentView.removeView(view);
                break;
            case ZoomView.CLICK_POINT_TYPE_CUT:
                showToast("剪切");
                break;
        }
    }

    @Override
    public void onFocus(View view) {
        switch (view.getId()) {
            case R.id.zoom_view:
                zoomView.setElevation(2);
                zoomView2.setElevation(1);
                break;
            case R.id.zoom_view2:
                zoomView.setElevation(1);
                zoomView2.setElevation(2);
                break;
        }
    }

    @Override
    public void onDoubleClick(View view) {
        switch (view.getId()) {
            case R.id.zoom_view:
                showToast("双击");
                break;
        }
    }
}
